package src;

import java.util.*;

/**
 * Die Klasse <code>Borrowing</code> implementiert ein ausgeliehenes Medium.
 * 
 * @author anon
 * @version 25.11
 * 
 */

// ############################## Datenfelder ##############################

public class Borrowing {
	private Date borrowingDate;
	private UserAccount borrowingUser;
	private Item borrowedItem;

// ############################## Konstruktoren ##############################

	public Borrowing(Item item, UserAccount user) {
		if (item == null || user == null)
			throw new NullPointerException();
		borrowingDate = new Date();
		borrowedItem = item;
		borrowingUser = user;
	}

// ############################## Methoden ##############################

	public double getOverdraftFee() {
		long now = System.currentTimeMillis();
		long diff = now - borrowingDate.getTime();
		if (diff > borrowedItem.getMaxBorrowTime()) {
			long days = (diff - borrowedItem.getMaxBorrowTime())
					/ (24 * 60 * 60 * 1000L); // Stunden, Minuten, Sekunden,
												// Millisekunden
			return days * borrowedItem.getFeePerDay();
		}
		return 0;
	}

	public UserAccount getBorrowingUserAccount() {
		return this.borrowingUser;
	}

	public Item getBorrowedItem() {
		return this.borrowedItem;
	}

	public Date getBorrowingDate() {
		return this.borrowingDate;
	}

	@Override
	protected void finalize() throws Throwable {
		try {
			borrowingUser.addToAccumulatedOverdraftFee(getOverdraftFee());
		} catch (Exception e) {
		} finally {
			super.finalize();
		}
	}

}
